package dpda;

import pda.PDAConfiguration;
import pda.PDARule;

import java.util.List;

/**
 * @author haoly
 * @title DPDARulebook
 * @date 2025/4/21 10:51
 * @description TODO
 */
public class DPDARulebook<T> {
    private final List<PDARule<T>> rules;

    public DPDARulebook(List<PDARule<T>> rules) {
        this.rules = rules;
    }

    public PDAConfiguration<T> nextConfiguration(PDAConfiguration<T> configuration, Character character){
        PDARule<T> rule = ruleFor(configuration, character);
        if (rule != null) {
            return rule.follow(configuration);
        }
        return null;
    }

    public PDARule<T> ruleFor(PDAConfiguration<T> configuration, Character character) {
        for (PDARule<T> rule : rules) {
            if (rule.appliesTo(configuration, character)) {
                return rule;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return "#struct DPDARulebook rules=[...]";
    }

    public Boolean appliesTo(PDAConfiguration<T> configuration, Character character){
        return ruleFor(configuration, character) != null;
    }

    public PDAConfiguration<T> followFreeMoves(PDAConfiguration<T> configuration){
        if (appliesTo(configuration, null)) {
            return followFreeMoves(nextConfiguration(configuration, null));
        }else {
            return configuration;
        }
    }
}
